package analyzemytime.anlyzemytime;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.joda.time.DateTime;

import analyzemytime.datamodel.Employee;
import analyzemytime.datamodel.Project;
import analyzemytime.datamodel.Report;

public class XMLReader {
	HashMap<String, Project> projectData = new HashMap<String, Project>();
	
	public ArrayList<Report> readXLSFile(String file){
	 	
		// HSSFWorkbook, File
		  NPOIFSFileSystem fs;
		  HSSFWorkbook wb = null;
		  ArrayList<Report> reports = new ArrayList<Report>();
			try {
				fs = new NPOIFSFileSystem(new File(file));
				wb = new HSSFWorkbook(fs.getRoot(), false);
				fs.close();
				Pattern pattern = Pattern.compile(".*?(\\w+)_(\\w+).*?");
	    		Matcher matcher = pattern.matcher(file);
				if(matcher.matches()){
					String firstName = matcher.group(2);
					String lastName = matcher.group(1);
					reports = getReports(wb,firstName,lastName);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return reports;
	    }
	    
	    public ArrayList<Report> getReports(Workbook wb,String firstName,String lastName){
	    	ArrayList<Report> reports = new ArrayList<Report>();
	    	Employee employee = new Employee(firstName, lastName);
	    	
	    	for (int i =0 ;i<wb.getNumberOfSheets();i++){
	    		Sheet sheet = wb.getSheetAt(i);
	    		if(projectData.get(sheet.getSheetName())==null){
	    			Project project = new Project(sheet.getSheetName());
	    			projectData.put(sheet.getSheetName(), project);
	    		}
	    		Project project = projectData.get(sheet.getSheetName());
	    		HashMap<DateTime, Double> timeReportedMap = new HashMap<DateTime, Double>();
	    		for(Row row : sheet){
	    			if(row.getCell(0).getCellType()==Cell.CELL_TYPE_NUMERIC){
	    				DateTime date = analyzemytime.datamodel.Utils.formatDateToDateTime(row.getCell(0).getDateCellValue());
	    				Double hoursReported = 0.0;
	    				hoursReported = row.getCell(2).getNumericCellValue();
	    
	    				if(timeReportedMap.get(date)==null){
	    					timeReportedMap.put(date, hoursReported);
	    				}else{
	    					double temp = timeReportedMap.get(date);
	    					timeReportedMap.put(date, hoursReported+temp);
	    				}
	    			}
	    		}
	    		Iterator<DateTime> iterator = timeReportedMap.keySet().iterator();
	    		while(iterator.hasNext()){
	    			DateTime date = iterator.next();
	    			Report report = new Report(employee, project,date ,timeReportedMap.get(date));
	    			reports.add(report);
	    		}
	    		
	    	}
	    	
	    	return reports;
	    }

}
